﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;

namespace Silvermoon.Windows
{
    public delegate void CommandHandler(object sender, CommandEventArgs command);

    /// <summary>
    /// Class which can be raised from any control and bubbles back to the root on each control if not handled.
    /// Finally it raises an event.
    /// </summary>
    public class Command : ICommand
    {
        #region fields
        private static int commandId;
        private RoutingStrategy routing = RoutingStrategy.Bubble;

        /// <summary>
        /// Identifies the command instance.
        /// </summary>
        public readonly int Id = commandId++;
        #endregion
        #region ctor

        public Command()
            : base()
        {
        }

        public Command(RoutingStrategy routing)
            : this()
        {
            this.routing = routing;
        }

        #endregion
        #region ICommand Members

        /// <summary>
        /// Gets or sets the routing strategy.
        /// </summary>
        public RoutingStrategy Routing
        {
            get { return routing; }
            set { routing = value; }
        }

        /// <summary>
        /// Executes the command.
        /// </summary>
        public virtual void Execute(object sender, CommandEventArgs e)
        {
            if (!e.Cancel && Executed != null) Executed(sender, e);
        }

        #endregion
        #region methods

        public override int GetHashCode()
        {
            return Id;
        }

        public override bool Equals(object obj)
        {
            Command c = obj as Command;
            if (c == null) return false;
            return c.Id == Id;
        }

        public override string ToString()
        {
            return "Command " + Id.ToString();
        }
        #endregion
        #region events

        /// <summary>
        /// Occurs when the command executes.
        /// </summary>
        public event EventHandler<CommandEventArgs> Executed;

        #endregion
    }

}
